package com.zzyl.controller.manager;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.AlertRuleDto;
import com.zzyl.service.AlertRuleService;
import com.zzyl.vo.AlertRuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/alert-rule")
@Api(tags = "报警规则相关接口")
public class AlertRuleController {

    @Autowired
    private AlertRuleService alertRuleService;

    @ApiOperation(value = "分页获取告警规则列表")
    @GetMapping("/get-page")
    public ResponseResult<PageResponse<AlertRuleVo>> getRulePage(Integer pageNum, Integer pageSize, String alertRuleName, String productKey, String functionName) {
        PageResponse<AlertRuleVo> pageResponse = alertRuleService.getAlertRulePage(pageNum, pageSize, alertRuleName, productKey, functionName);
        return ResponseResult.success(pageResponse);
    }

    @ApiOperation(value = "新增报警规则")
    @PostMapping("/create")
    public ResponseResult<Void> createRule(@RequestBody AlertRuleDto alertRuleDto) {
        alertRuleService.createRule(alertRuleDto);
        return ResponseResult.success();
    }

    @ApiOperation(value = "获取单个告警规则")
    @GetMapping("/read/{id}")
    public ResponseResult<AlertRuleVo> readRule(@PathVariable Long id) {
        AlertRuleVo alertRuleVo = alertRuleService.readAlertRule(id);
        return ResponseResult.success(alertRuleVo);
    }

    @ApiOperation(value = "更新告警规则")
    @PutMapping("/update/{id}")
    public ResponseResult<Void> updateRule(@PathVariable Long id, @RequestBody AlertRuleDto alertRuleDto) {
        alertRuleService.updateAlertRule(id, alertRuleDto);
        return ResponseResult.success();
    }

    @ApiOperation(value = "删除告警规则")
    @DeleteMapping("/delete/{id}")
    public ResponseResult<Void> deleteRule(@PathVariable Long id) {
        alertRuleService.deleteAlertRule(id);
        return ResponseResult.success();
    }

    @ApiOperation(value = "启用/禁用")
    @PutMapping("/{id}/status/{status}")
    public ResponseResult<Void> enableOrDisable(@PathVariable Long id, @PathVariable Integer status) {
        alertRuleService.enableOrDisable(id, status);
        return ResponseResult.success();
    }
}
